package com.bdqn.systems.auth;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author : CP
 * @version: 1.0
 * @program : spring-security-test
 * @description : json认证方式
 * @date : 2020-12-18 09:31
 **/
public class JSONAuthentication {


    protected void WriteJSON(HttpServletRequest request,
                             HttpServletResponse response,
                             Object data) throws IOException {
        // 设置请求头
        response.setContentType("application/json;charset=UTF-8");
        // 设置跨域方案
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Method","POST");

        // 输出JSON
        PrintWriter out = response.getWriter();
        // 将data转换为json对象或数组
        out.write(new ObjectMapper().writeValueAsString(data));
        out.flush();
        out.close();
    }
}
